
#ifndef SCRIPTSTUDIO_H
#define SCRIPTSTUDIO_H

#include <BSet>
#include <BMap>
#include <BWidget>
#include <BImage>
#include <BSlider>
#include <BTabWidget>
#include <BTreeWidget>
#include <BSonDocument>
#include <BTextEdit>
#include <BMenu>
#include <BLabel>
#include <BButton>
#include <BLineEdit>
#include <BDialog>
#include <BComboBox>
#include <BClass>
#include <BScript>

#include "SourceEdit.h"
#include "ScriptBrowser.h"

class ScriptStudio : public BDialog
{
	BOBJECT(ScriptStudio)
public:
	ScriptStudio(BWidget* parent = 0);
	~ScriptStudio();

	bool loadProject(const BString& fileName);
	bool saveProject(const BString& fileName);

protected:
	void initScript(BScript* script);
	BTreeItem* createScriptItem(BScript* script);
	BTextEdit* openTreeItem(BTreeItem* item);
	void freshTreeItem(BTreeItem* item);
	void drawTreeItem(BPainter& painter, BTreeItem* item);
	void collectDepends(BExp* exp, SourceInfo* srcinfo);
	void dirtyDependSource(const BString& header);
	void jumpToBreakpoint(BScript* script);
	void updateEntrance(BScript* script);
	void freshScriptStack(BScript* script);
	void freshVariantButton(const BVariant& var);
	BObject* createMenuOrAction(const BVariant& var);

protected:
	void slotOpenActionTrigger(BAction* action);
	void slotAddFileActionTrigger(BAction* action);
	void slotAddFolderActionTrigger(BAction* action);
	void slotAddScriptActionTrigger(BAction* action);
	void slotNewFileActionTrigger(BAction* action);
	void slotNewScriptActionTrigger(BAction* action);
	void slotNewFolderActionTrigger(BAction* action);
	void slotRemoveActionTrigger(BAction* action);
	void slotBrowseActionTrigger(BAction* action);
	void slotCleanActionTrigger(BAction* action);

	void slotOpenButtonClicked();
	void slotSaveButtonClicked();
	void slotUndoButtonClicked();
	void slotRedoButtonClicked();
	void slotSearchButtonToggled(BObject* object, const BValue& value);
	void slotBuildButtonToggled(BObject* object, const BValue& value);
	void slotDebugButtonToggled(BObject* object, const BValue& value);
	void slotDebugResetButtonClicked(BObject* object, const BValue& value);
	void slotDebugCallButtonClicked(BObject* object, const BValue& value);
	void slotDebugRunButtonClicked(BObject* object, const BValue& value);
	void slotDebugNextButtonClicked();
	void slotDebugStepButtonClicked();
	void slotDebugStopButtonClicked();
	void slotDebugFunctionChanged(BObject* object, const BValue& value);
	void slotDebugEditFramed(BObject* object, const BValue& value);
	void slotPopupVariantButton(BObject* object, const BValue& value);

	void slotScriptCompiled(BObject* object, const BValue& value);
	void slotScriptMessage(BObject* object, const BValue& value);
	void slotScriptBreaked(BObject* object, const BValue& value);
	void slotScriptInited(BObject* object, const BValue& value);
	void slotScriptError(BObject* object, const BValue& value);

	void slotTreeWidgetFramed(BObject* object, const BValue& value);
	void slotTreeWidgetRightClicked(BObject* object, const BValue& value);
	void slotTreeItemSelected(BObject* object, const BValue& value);
	void slotTreeItemClicked(BObject* object, const BValue& value);
	void slotTreeItemDoubleClicked(BObject* object, const BValue& value);

	void slotItemEditFocused(BObject* object, const BValue& value);
	void slotItemTextEdited(BObject* object, const BValue& value);
	void slotItemEditFinished(BObject* object, const BValue& value);

	void slotCurrentTabChanged(BObject* object, const BValue& value);
	void slotSourceTextEdited(BObject* object, const BValue& vlaue);
	void slotOutputDoubleClicked(BObject* object, const BValue& value);
	void slotCreateBreakpoint(BObject* object, const BValue& value);
	void slotRemoveBreakpoint(BObject* object, const BValue& value);

protected:
	virtual void updateEvent(const BEvent& event);

	virtual void showEvent(const BEvent& event);
	virtual void closeEvent(const BEvent& event);

	virtual void resizeEvent(const BEvent& event);

	virtual void styleEvent(const BEvent& event);
	virtual void freshEvent(const BEvent& event);
	virtual void paintEvent(const BEvent& event);

private:
	BMenu*				_itemMenu;
	BWidget*			_toolBar;
	BButton*			_openButton;
	BButton*			_saveButton;
	BButton*			_undoButton;
	BButton*			_redoButton;
	BButton*			_searchButton;
	BButton*			_buildButton;
	BButton*			_debugButton;
	BButton*			_debugCallButton;
	BButton*			_debugRunButton;
	BButton*			_debugNextButton;
	BButton*			_debugStepButton;
	BButton*			_debugStopButton;
	BButton*			_debugResetButton;
	BComboBox*			_functionComboBox;

	BLabel*				_treeLabel;
	BTreeWidget*		_treeWidget;
	BTabWidget*			_tabWidget;
	BTabWidget*			_outputTabWidget;

	BHolder<BButton>	_variantButton;
	BLineEdit*			_itemEdit;
	BPoint				_pressedPos;

	BValue					_stackValue;
	BHolder<BTextEdit>		_debugEdit;
	int						_debugLine;
	BString					_breakpoint;
	BImageHolder			_arrowImage;
	BHolder<const BImage>	_warnImage;
};

#endif